<script setup lang="ts" name="ImpotGen">
import { importTableApi, listDbTableApi } from "@/api/tool/gen";

const { proxy } = getCurrentInstance();

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  title: {
    type: String,
    default: false,
  }
});

const selection = ref<GenTableRecord[]>([])

// 导入表 表单
const queryGenRef = ref(null);

// 弹窗开关
const open = ref(false);

// 搜索栏
const queryParams = ref<GenTableRecord>({
  pageNum: 1,
  pageSize: 10,
  tableName: null,
  tableComment: null
})

const dbTableList = ref<GenTableRecord[]>([]);

// 加载
const loading = ref(false);

// 总条数
const total = ref(0);

/** 获取定时任务详情 */
function getData() {
  open.value = true;
  getList();
}

/** 查询表数据 */
function getList() {
  listDbTableApi(queryParams.value).then(res => {
    dbTableList.value = res.rows;
    total.value = res.total;
    dbTableList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    })
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryGenRef.value.resetFields()
  handleQuery();
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: GenTableRecord[]) {
  selection.value = selectionList;
}

/** 提交按钮 */
function submitForm() {
  if (!selection.value.length) {
    proxy.$modal.msgError("请选择要导入的表");
    return;
  }
  let tables = selection.value.map(item => item.tableName);
  loading.value = true;
  importTableApi({ tables }).then(response => {
    proxy.$modal.msgSuccess(response.msg);
    if (response.code === 200) {
      loading.value = false;
      closeDialog(true);
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
});

getData();
</script>

<template>
  <!-- 定时任务编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="900px" append-to-body :close-on-click-modal="false">
    <el-form ref="queryGenRef" :model="queryParams" :inline="true" label-width="80px">
      <el-form-item label="表名称" prop="tableName">
        <el-input
            v-model="queryParams.tableName"
            placeholder="请输入表名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
            v-model="queryParams.tableComment"
            placeholder="请输入表描述"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item class="item-search">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
        ref="table"
        v-loading="loading"
        :data="dbTableList"
        @selection-change="handleSelectionChange"
        height="440px"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" align="center" width="55" prop="colNo"/>
      <el-table-column prop="tableName" label="表名称" show-overflow-tooltip />
      <el-table-column prop="tableComment" label="表描述" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateTime" label="更新时间" />
    </el-table>
    <pagination
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>